from .Linear_Parameter import Linear_Parameter
from .Step_size import Step_size
from .Trunk_Composition import Trunk_Composition
from .signal_attenuation import signal_attenuation
from .Dispersion import Dispersion
from .nonlinearity_step import nonlinearity_step
def SSFM_each_span(sig, Signal, Fiber_para, PMD_para):
    data = sig
    stepnumber = Fiber_para['stepnumber']
    L = Fiber_para['L']
    step_type = Fiber_para['step_type']

    # 单个跨段总的已仿真长度
    Final_distance = 0

    # 构造参数
    # 线性和非线性参数
    Signal, Fiber_para, PMD_para = Linear_Parameter(Signal, Fiber_para, PMD_para)

    # [Signal,Fiber_para,PMD_para] = Nonlinear_Parameter(Signal,Fiber_para,PMD_para);
    # Nonlinear_Parameter 函数的调用

    # SSFM主体部分

    while Final_distance < L:
        # 步长索引
        Fiber_para['i'] += 1
        Dis = 1
        # 更新步长
        StepSize = Step_size(data, Signal, Fiber_para, PMD_para)
        Fiber_para['step_size'] = StepSize
        # 记录长度
        Final_distance += StepSize
        # 如果Final_distance超过L的长度，最后一步改为Final_distance - L
        if Final_distance > L:
            Fiber_para['step_size'] = Final_distance - L
        # 构造偏振模色散参数

        PMD_para = Trunk_Composition(Final_distance, StepSize, Fiber_para, PMD_para)

        # 1/2 衰减
        signal_atten_1 = signal_attenuation(data, Signal, Fiber_para, PMD_para)
        # 1/2 线性
        signal_lstep_1 = Dispersion(signal_atten_1, Signal, Fiber_para, PMD_para, Dis)
        Dis += 1
        # 非线性
        signal_nstep = nonlinearity_step(signal_lstep_1, Signal, Fiber_para)
        # 1/2 线性
        signal_lstep_2 = Dispersion(signal_nstep, Signal, Fiber_para, PMD_para, Dis)
        # 1/2 衰减
        signal_atten_2 = signal_attenuation(signal_lstep_2, Signal, Fiber_para, PMD_para)

        data = signal_atten_2

        # 输出当前的步长的类型、步长索引、步长长度、总长度
        # print(f'{step_type:<12} {Fiber_para["i"]:<8d} {StepSize:<12.5f} {Final_distance:<9.5f}')

    signal = data

    return signal
